package com.bnzj.cloud.business.core.web;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bnzj.cloud.business.core.persistence.entity.SysJobLog;
import com.bnzj.cloud.business.core.service.SysJobLogService;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.webmvc.util.WebMvcUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @author Barry
 * @createDate 2020/2/17
 * @description
 */
@Tag(name = "定时任务执行日志", description = "定时任务执行日志表管理")
@RestController
@RequestMapping("/system/job/log")
public class SysJobLogController {

    @Autowired
    private SysJobLogService sysJobLogService;

    @Operation(summary = "分页查询")
    @GetMapping(value = "/page")
    public ResponseResult<Page<SysJobLog>> getJobLogPage(HttpServletRequest request, @Parameter(description = "任务名称") @RequestParam(required = false) String name) {
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "log.start_time"));
        QueryHelpBean queryHelpBean = SysJobLogService.getQueryHelpBean();
        queryHelpBean.setPageable(pageable);
        IExpression orCondition1 = CompareExpression.build("log.name", CompareType.equal, name);
        queryHelpBean.addWhere(orCondition1);
        Page<SysJobLog> all = sysJobLogService.findAll(queryHelpBean);
        return ResponseResult.success(all);
    }

    @Operation(summary = "根据id查询")
    @GetMapping("/one/{id}")
    public ResponseResult<SysJobLog> getById(@Parameter(in = ParameterIn.PATH, name = "id", description = "主键", required = true) @PathVariable("id") Long id) {
        Optional<SysJobLog> optional = sysJobLogService.findById(id);
        return ResponseResult.success(optional.orElse(null));
    }
}
